package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.project.bean.FacilityBean;

import com.project.bean.RoomBean;
import com.project.mapper.IFacilityMapper;
import com.project.mapper.IRoomMapper;
import com.project.service.IFacilityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
@Transactional
public class FacilityServiceImpl implements IFacilityService {
    @Autowired
    private IFacilityMapper facMapper;
    @Autowired
    private IRoomMapper roomMapper;
    @Override
    public void addFac(FacilityBean facility) {
        facMapper.insert(facility);
        RoomBean room = roomMapper.selectById(facility.getRoomId());
        room.setRoomStatus("设施损坏");
        roomMapper.updateById(room);
    }

    @Override
    public void solveFac(Integer facId, Integer roomId) {
//        int num = 0;
//        facMapper.changeStatus(facId,"已解决");
//        num= facMapper.findFacilitiesNum(roomId,"未解决");
//        if (num==0){
//            roomMapper.updateRoomStatus(roomId,"正常");
//         }
        //得到设施ID对应设施对象
        FacilityBean facility = facMapper.selectById(facId);
        facility.setFacStatus("已解决");
        facMapper.updateById(facility);

        QueryWrapper<FacilityBean> qw = new QueryWrapper<>();
        qw.eq("f_status","未解决");
        qw.eq("fk_roomId",roomId);

        Integer num = facMapper.selectCount(qw);
        if(num == 0){
            RoomBean room = roomMapper.selectById(roomId);
            room.setRoomStatus("正常");
            roomMapper.updateById(room);
        }
    }

    @Override
    public List<FacilityBean> findFacById(Integer roomId) {
        QueryWrapper<FacilityBean> qw = new QueryWrapper<>();
        qw.eq("fk_roomId",roomId);
        return facMapper.selectList(qw);
    }
}
